package com.cl.mybatis.learn.day17;

import java.io.InputStream;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.cl.mybatis.learn.day17.bean.Orders;
import com.cl.mybatis.learn.day17.bean.User;
import com.cl.mybatis.learn.day17.dao.OrdersMapper;
import com.cl.mybatis.learn.day17.dao.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Mybatis001 {
    public static void main(String[] args) throws Exception {
        
        String resource = "common1/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        OrdersMapper ordersMapper = session.getMapper(OrdersMapper.class);
        
        UserMapper userMapper = session.getMapper(UserMapper.class);
        
        try {
            Orders orders = ordersMapper.selectById(12);
            Object obj = JSONArray.toJSON(orders);
            String json = obj.toString();
            System.out.println("orders：" + json);
            
            List<User> users = userMapper.selectAll();
            obj = JSONArray.toJSON(users);
            json = obj.toString();
            System.out.println("users：" + json);
            
        } finally {
            session.close();
        }
        
    }
    
}
